package com.picket.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;
import com.picket.service.UserPoolService;

@Controller
public class LandingController extends BaseController {

	private UserPoolService userPoolService;

	@RequestMapping(value="/landing",method = RequestMethod.GET)
	public String getPoolLanding(Model model) {
		final User user = userService.getCurrentUser();
		if (user != null && user.getEmail() != null) {
			model.addAttribute("userPools",
					userPoolService.findByEmail(user.getEmail()));
		} else {

		}

		return "/landing";
	}
	
	@RequestMapping(value = "/landing/logout", method = RequestMethod.GET)
	public void logout(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		request.getSession().invalidate();

		String logoutUrl = UserServiceFactory.getUserService().createLogoutURL(
				"/landing/loggedout");

		response.sendRedirect(logoutUrl);
	}

	@RequestMapping(value = "/landing/loggedout", method = RequestMethod.GET)
	public String loggedOut() {
		return "/loggedout";
	}


	public UserPoolService getUserPoolService() {
		return userPoolService;
	}

	public void setUserPoolService(UserPoolService userPoolService) {
		this.userPoolService = userPoolService;
	}
}
